import _Blob from 'miniprogram-blob';
import _File from 'miniprogram-file';
import _FormData from 'miniprogram-formdata';

export default function createPolyfills({ environment = false } = { environment: true }) {
    var polyfills = {};
    if (typeof Blob !== "undefined") {
        polyfills.Blob = Blob;
    } else {
        polyfills.Blob = _Blob;
        if (environment) {
            if (typeof globalThis !== "object") {
                Object.defineProperty(Object.prototype, "Blob", {
                    get() {
                        return _Blob;
                    },
                });
            } else {
                globalThis.Blob = _Blob;
            }
        }
    }
    if (typeof File !== "undefined") {
        polyfills.File = File;
    } else {
        polyfills.File = _File;
        if (environment) {
            if (typeof globalThis !== "object") {
                Object.defineProperty(Object.prototype, "File", {
                    get() {
                        return _File;
                    },
                });
            } else {
                globalThis.File = _File;
            }
        }
    }
    if (typeof FormData !== "undefined") {
        polyfills.FormData = FormData;
    } else {
        polyfills.FormData = _FormData;
        if (environment) {
            if (typeof globalThis !== "object") {
                Object.defineProperty(Object.prototype, "FormData", {
                    get() {
                        return _FormData;
                    },
                });
            } else {
                globalThis.FormData = _FormData;
            }
        }
    }
    return polyfills;
}

export const polyfills = createPolyfills({ environment: false });